package com.cx.purchase.legalplant.service;

import com.cx.purchase.config.ResultVO;
import com.cx.purchase.config.ResultVOBuilder;
import com.cx.purchase.legalplant.mapper.LegalPlantMapper;
import com.cx.purchase.legalplant.model.*;
import com.cx.purchase.plan.model.PlanClass;
import com.cx.purchase.subclass.model.Subclass;
import com.cx.purchase.util.AllrecordsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.beans.Transient;
import java.util.Date;
import java.util.List;

@Service
public class LegalPlantServiceImpl implements LegalPlantService{
    @Autowired
    private LegalPlantMapper legalPlantMapper;




    @Override
    public ResultVO queryPage(LegalPlantSelectVo legalPlantSelectVo) {

        List<LegalPlantVO> List = legalPlantMapper.queryData(legalPlantSelectVo);

     /*   for (LegalPlantVO legalPlantVO :
                List) {
            if (legalPlantVO.getEnabled()) {
                legalPlantVO.setEnabledName("启用");
            }else{
                legalPlantVO.setEnabledName("禁用");
            }

        }*/

        int count =  legalPlantMapper.queryCount(legalPlantSelectVo);

        AllrecordsVo allrecordsVo = new AllrecordsVo();
        allrecordsVo.setPageIndex(legalPlantSelectVo.getPageIndex());
        allrecordsVo.setPageSize(legalPlantSelectVo.getPageSize());
        allrecordsVo.setTotalNumber(count);
        allrecordsVo.setDataList(List);
        allrecordsVo.resettotalNum(count);

        return ResultVOBuilder.success(allrecordsVo);


    }

    @Override
    public ResultVO queryLegalPerson(Integer id) {
        List<LegalPersonVO> legalPersonVO = legalPlantMapper.query(id);

        return ResultVOBuilder.success(legalPersonVO);
    }

    @Transactional
    @Override
    public ResultVO update(LegalPlantUpdateVO legalPlantUpdateVO) {
        LegalPlantVO legalPlantVO = legalPlantMapper.queryByCode(legalPlantUpdateVO.getLegalPlantCode());
        if (legalPlantVO==null){
            return ResultVOBuilder.error("修改数据不存在!");
        }

        Date nowDate = new Date();
        LegalPlantVO plantVo1 = new LegalPlantVO();
        BeanUtils.copyProperties(legalPlantUpdateVO,plantVo1);
        plantVo1.setUpdateName("张三");
        plantVo1.setUpdateTime(nowDate);
        legalPlantMapper.update(plantVo1);
        return ResultVOBuilder.success();


    }

    @Override
    public ResultVO updateStatus(LegalPlantUpdateStatus legalPlantUpdateStatus) {
      /*  LegalPlantUpdateStatus legalPlantUpdateStatus1=legalPlantMapper.queryCode(legalPlantUpdateStatus.getLegalPersonCode());
        if(legalPlantUpdateStatus1==null){
            return ResultVOBuilder.error("修改的数据不存在,请检查数据");
        }*/
        String name = "张三";
        Date date = new Date();
        LegalPlantVO legalPlantVO1 = new LegalPlantVO();

        //由于目前对象没有添加的功能所以将添加的功能付给 目前的这个对象
        BeanUtils.copyProperties(legalPlantUpdateStatus,legalPlantVO1);

        legalPlantVO1.setUpdateName(name);
        legalPlantVO1.setUpdateTime(date);

        System.out.println(legalPlantVO1);

        legalPlantMapper.updateStatus(legalPlantVO1);

        return ResultVOBuilder.success();

    }

    @Transactional
    @Override
    public ResultVO add(@Valid LegalPlantAddVO legalPlantAddVO) {


        LegalPlantVO legalPlantVO= legalPlantMapper.queryByCode(legalPlantAddVO.getLegalPlantCode());
        if(legalPlantVO!=null){
            return ResultVOBuilder.error("已存在改编码,请检查数据");
        }
      /*  LegalPersonVO legalPersonVO1 = legalPlantMapper.queryByPersonCode(legalPlantAddVO.getLegalPersonCode());
        if(legalPersonVO1==null){
            return ResultVOBuilder.error("法人不存在");
        }*/

        String name = "张三";
        Date date = new Date();
        LegalPlantVO legalPlantVO1 = new LegalPlantVO();

        //由于目前对象没有添加的功能所以将添加的功能付给 目前的这个对象
        BeanUtils.copyProperties(legalPlantAddVO,legalPlantVO1);

        legalPlantVO1.setCreateName(name);
        legalPlantVO1.setCreateTime(date);
        legalPlantVO1.setUpdateName(name);
        legalPlantVO1.setUpdateTime(date);


        legalPlantMapper.add(legalPlantVO1);

        return ResultVOBuilder.success();
    }






}
